package com.acme.bankapp.service;

import java.io.IOException;

import com.acme.bankapp.dal.BankDataService;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.domain.client.ClientExistsException;
import com.acme.bankapp.domain.email.Email;
import com.acme.bankapp.service.email.EmailService;

public class BankService {
	public static void addClient(Bank bank, Client client)
			throws ClientExistsException, IOException {
		bank.addClient(client);
		BankDataService.saveBankToFile(bank);
	}

	@SuppressWarnings("serial")
	public static void enableEmailNotifications(Bank bank) {
		bank.addClientRegistrationListener(new ClientRegistrationListener() {

			public void onClientAdded(Client c) {
				String msg = String.format("Client %s added",
						c.getClientSalutation());
				Email email = new Email(msg, "admin@localhost");
				EmailService emailSvc = EmailService.getInstance();
				emailSvc.sendNotificationEmail(email);
			}

		});
	}

}
